﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using LibrarySystem.Models;
using LibrarySystem.LibraryUtils.Entities;

namespace LibrarySystem.LibraryUtils.DAL.DAOs
{
    public class Library_Skippings : Common
    {

        public int GetDataCount(string borrowingID = null, int employerID = 0)
        {
            LibraryEntitiesDataContext _context = base.GetDataContext();
            //Code
            IEnumerable<Library_Skipping> querySkip = _context.Library_Skippings;
            //Filter
            if (!string.IsNullOrEmpty(borrowingID))
            {
                querySkip = querySkip.Where(ls => ls.Library_Borrowing.Id.ToString().Contains(borrowingID));
            }
            if (employerID > 0)
            {
                querySkip = querySkip.Where(ls => ls.Library_Employee.Id == employerID);
            }
            return querySkip.Count();
        }

        public List<SkippingModel> GetDataList(int startIndex = 0, int pageSize = 0, string sorting = null,
            string borrowingID = null, int employerID = 0)
        {
            LibraryEntitiesDataContext _context = base.GetDataContext();
            //Code
            IEnumerable<Library_Skipping> querySkip = _context.Library_Skippings;
            //Filter
            if (!string.IsNullOrEmpty(borrowingID))
            {
                querySkip = querySkip.Where(ls => ls.Library_Borrowing.Id.ToString().Contains(borrowingID));
            }
            if (employerID > 0)
            {
                querySkip = querySkip.Where(ls => ls.Library_Employee.Id == employerID);
            }
            //Sorting
            if (string.IsNullOrEmpty(sorting) || sorting.Equals("BorrowingID ASC"))
            {
                querySkip = querySkip.OrderBy(ls => ls.BorrowingId);
            }
            else if (sorting.Equals("BorrowingID DESC"))
            {
                querySkip = querySkip.OrderByDescending(ls => ls.BorrowingId);
            }
            //
            List<Library_Skipping> skipping = pageSize > 0 ?
                                                querySkip.Skip(startIndex).Take(pageSize).ToList()
                                                : querySkip.ToList();
            List<SkippingModel> skipList = new List<SkippingModel>();
            foreach (Library_Skipping sk in skipping)
            {
                SkippingModel data = new SkippingModel();
                data.ID = sk.Id;
                data.BorrowingID = sk.Library_Borrowing.Id;
                data.EmployerID = sk.Library_Employee.Id;
                data.Reason = sk.Reason;
                data.SkippedDate = (DateTime)sk.SkippedDate;
                data.CheckAgreed = (Boolean)sk.CheckAgreed;
                skipList.Add(data);
            }
            _context.Dispose();
            return skipList;
        }

        public SkippingModel AddNewData(SkippingModel data)
        {
            LibraryEntitiesDataContext _context = base.GetDataContext();
            //Code
            Library_Skipping skip = new Library_Skipping();
            //Assign Param
            skip.BorrowingId = data.BorrowingID;
            skip.EmployerId = data.EmployerID;
            skip.Reason = data.Reason;
            skip.SkippedDate = (DateTime)data.SkippedDate;
            data.CheckAgreed = (bool)data.CheckAgreed ? true : false;
            //Insert
            _context.Library_Skippings.InsertOnSubmit(skip);
            _context.SubmitChanges();
            //Assign new ID to Model
            data.ID = skip.Id;
            //
            _context.Dispose();
            return data;
        }

        public bool UpdateData(SkippingModel data)
        {
            LibraryEntitiesDataContext _context = base.GetDataContext();
            //Code
            Library_Skipping skip = _context.Library_Skippings.Where(sk => sk.Id == data.ID).FirstOrDefault();
            //Assign Param
            skip.Reason = data.Reason;
            skip.SkippedDate = (DateTime)data.SkippedDate;
            skip.CheckAgreed = (bool)data.CheckAgreed ? true : false;
            //Update
            _context.SubmitChanges();
            //
            _context.Dispose();
            return true;
        }

        public bool DeleteData(int Id)
        {
            LibraryEntitiesDataContext _context = base.GetDataContext();
            //Code
            Library_Skipping skip = _context.Library_Skippings.Where(sk => sk.Id == Id).FirstOrDefault();
            //
            IEnumerable<Library_SkippingDetail> deletingList = _context.Library_SkippingDetails.Where(d => d.SkippingId == skip.Id);
            _context.Library_SkippingDetails.DeleteAllOnSubmit(deletingList);
            _context.SubmitChanges();
            //
            IEnumerable<Library_Receipt> receipList = _context.Library_Receipts.Where(r => r.SkippingId == skip.Id);
            _context.Library_Receipts.DeleteAllOnSubmit(receipList);
            _context.SubmitChanges();
            //Delete
            _context.Library_Skippings.DeleteOnSubmit(skip);
            _context.SubmitChanges();
            //
            _context.Dispose();
            return true;
        }
    }
}